<?php

	function main($time_ini,$time_end) { ?>
	<table width="100%"  border="0" cellpadding="10" cellspacing="1">
	   <tr bgcolor="#009999">
          <td colspan="3">Totais entre <?php echo time_output($time_ini);?> e <?php echo time_output($time_end);?></td>
</tr>
        <?php

	$QViewDays2 = "SELECT *

	FROM exchange_operations 
	WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}' AS firsttime
	SET firsttime INTERVAL 86400 AS time
	GROUP BY time";
	$ViewDays2 = $GLOBALS['CONN']->db_array($QViewDays2); 
	for ($vi = 1; $vi <= $ViewDays2[0]; $vi++) { 
	
	?>
        <tr bgcolor="#F2F2F2">
		<form action="index.php" method="post">
          <td valign="top">Data:
                <?php echo date_output($ViewDays2[$vi]['operation_time']);?>
          </td>
          <td valign="top">Total:
		  
		  </td>
          <td valign="top">
		  <?php
		  $time_ini2 = mktime(0,0,0,$ViewDays2[$vi]['operation_time'],$ViewDays2[$vi]['operation_time'],$ViewDays2[$vi]['operation_time']);
		  $time_end2 = mktime(23,59,59,$ViewDays2[$vi]['operation_time'],$ViewDays2[$vi]['operation_time'],$ViewDays2[$vi]['operation_time']);
		  ?>
		  <input type="hidden" value="<?php echo $time_ini2;?>" name="time_ini2">
		  <input type="hidden" value="<?php echo $time_end2;?>" name="time_end2">
		  <input type="hidden" value="Days" name="View">
		  <input name="Detalhes" type="submit" id="Detalhes" value="Lojas">
		  </td></form>
        </tr>
        <?php
		} ?>
	
	
	</table>
	<?php }
	
	function days($time_ini,$time_end,$CSS) { ?>
	<table width="100%"  border="0" cellpadding="10" cellspacing="1">
 	<tr bgcolor="#009999">
          <td colspan="3">Lista de valores por loja</td>
        </tr>
        <?php
	$QCallMainLoja = "SELECT * FROM exchange_operations
	WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}'
	GROUP BY main_office_ref";
	
	$CallMainLoja = $GLOBALS['CONN']->db_array($QCallMainLoja);
	for ($v = 1; $v <= $CallMainLoja[0]; $v++) { 
	 ?>
        <tr bgcolor="#F9F9F9">
          <td valign="top">
	<?php
	$QCallSubLoja = "SELECT * FROM main_offices
	WHERE ref = '{$CallMainLoja[$v]['main_office_ref']}'";
	
	$CallSubLoja = $GLOBALS['CONN']->db_array($QCallSubLoja);
	for ($s = 1; $s <= $CallSubLoja[0]; $s++) { 
 
	 ?> 
	 <form action="index.php" method="post">
		  <?php echo $CallSubLoja[$s]['office_number'];?><b> | </b><?php echo $CallSubLoja[$s]['name'];?><b> | </b><?php echo $CallSubLoja[$s]['address_city'];?>
		  	 		
		  <input type="hidden" value="<?php echo $CallMainLoja[$v]['main_office_ref'];?>" name="office">
		  <input type="hidden" value="<?php echo $time_ini;?>" name="time_ini">
		  <input type="hidden" value="<?php echo $time_end;?>" name="time_end">
		   
		  <input type="hidden" value="Offices" name="View">
		  <?php 	if ($CSS['PrintClass'] != "Noforms") { ?>
		  <input name="Detalhes" type="submit" id="Detalhes" value="Detalhes">
		  <?php } ?>
		</form>
		  <?php 
	 } ?>
		  </td>
          <td valign="top">
	<?php 
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//TOTAL LOJA
	
	$QCallMain = "SELECT * FROM exchange_operations WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}' AND main_office_ref = '{$CallMainLoja[$v]['main_office_ref']}' GROUP BY type_ref";
	$CallMain = $GLOBALS['CONN']->db_array($QCallMain);
	for ($m = 1; $m <= $CallMain[0]; $m++) { 
	$Total = 0;

	if ($m == 1) {$condition = 'out_currency_ref';} elseif ($m == 2) {$condition = 'in_currency_ref';} 
	if ($m == 1) {$condition2 = 'in_currency_ref';} elseif ($m == 2) {$condition2 = 'out_currency_ref';} 
	$QCallSub = "SELECT * FROM exchange_operations 
	WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}' 
	AND main_office_ref = '{$CallMainLoja[$v]['main_office_ref']}'
	AND type_ref = '{$m}' 
	GROUP BY {$condition2}";
	$CallSub = $GLOBALS['CONN']->db_array($QCallSub);
	for ($n = 1; $n <= $CallSub[0]; $n++) { 
	
			$QCallTotalAmount = "SELECT SUM(Amount) as Amount, tax1, operation_time, in_currency_ref, out_currency_ref FROM exchange_operations
			WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}' 
			AND main_office_ref = '{$CallMainLoja[$v]['main_office_ref']}' 
			AND type_ref = '{$m}' 
			AND {$condition} = '{$CallSub[$n][$condition]}' AND {$condition2} = '{$CallSub[$n][$condition2]}'
			GROUP BY tax1";
			$CallTotalAmount = $GLOBALS['CONN']->db_array($QCallTotalAmount);
			for($x = 1; $x <= $CallTotalAmount[0]; $x++) {
			
		// Cacula Totais
		if ($m == 1 && $condition == "in_currency_ref") {$taxcalc = "buy1";}
		elseif ($m == 1 && $condition2 == "in_currency_ref") {$taxcalc = "buy2";}
		elseif ($m == 2 && $condition == "in_currency_ref") {$taxcalc = "sell1";}
		elseif ($m == 2 && $condition2 == "in_currency_ref") {$taxcalc = "sell2";}
		
		if ($condition == "in_currency_ref") {$regcalc = $CallTotalAmount[$x]['in_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['out_currency_ref'];
		}
		elseif ($condition == "out_currency_ref") {$regcalc = $CallTotalAmount[$x]['out_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['in_currency_ref'];
		}
		$QCallTot = "SELECT * FROM exchange_rates
			WHERE rate_time <= '{$CallTotalAmount[$x]['operation_time']}'
			AND (ref_currency1 = '{$regcalc}' AND ref_currency2 = '{$regcalc2}')
			OR (ref_currency2 = '{$regcalc}' AND ref_currency1 = '{$regcalc2}')
			ORDER BY rate_time DESC LIMIT 0,1";
			$CallTot = $GLOBALS['CONN']->db_array($QCallTot);
	 $minitot = $CallTotalAmount[$x]['Amount']*$CallTot[1][$taxcalc];
	 $Total = $Total+$minitot;
	
	 }
			$QCallTotalPayed = "SELECT SUM(payed) as payed, tax1 FROM exchange_operations
			WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}'
			AND main_office_ref = '{$CallMainLoja[$v]['main_office_ref']}' 
			AND type_ref = '{$m}' 
			AND {$condition} = '{$CallSub[$n][$condition]}' AND {$condition2} = '{$CallSub[$n][$condition2]}'
			GROUP BY tax1";
			$CallTotalPayed = $GLOBALS['CONN']->db_array($QCallTotalPayed);
			for($y = 1; $y <= $CallTotalPayed[0]; $y++) {
			$CallTotalPayed[$y]['tax1']*$CallTotalPayed[$y]['acerto']; 
		 }
	} 
	print "Total: ".number_format($Total,2).currency_output($CallMain[$m][$condition],'icon')." ".currency_output($CallMain[$m][$condition],'name');
}
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ?></td>
          <td valign="top">
		
		 </td>
        </tr>
        <?php } ?>
	</table>
<?php	
	}
	
	function offices($time_ini,$time_end,$V,$office,$CSS) { 
	?>
	<table width="100%"  border="0" cellpadding="10" cellspacing="1">
      <tr bgcolor="#009999">
          <td colspan="2"><?php 
		  if($V == "B" && $CSS['PrintClass'] != "Noforms") { print
		  "<form accept-charset='utf-8' action=\"index.php\" method=\"post\">";
		 print "Data:";
		 print  date_output($GLOBALS['T']);
		 print " <input type=\"hidden\" value=\"{$time_ini}\" name=\"time_ini\">
		  <input type=\"hidden\" value=\"{$time_end}\" name=\"time_end\">
		  <input type=\"hidden\" value=\"{$office}\" name=\"office\">
		  <input type=\"hidden\" value=\"Details\" name=\"View\">
		  <input  name=\"Details\" type=\"submit\" value=\"Detalhes das Opera��es\">
		  </form>";
		  
		  }?></td>
      </tr>
	 
	<?php 
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// VISTA POR LOJA | TOTAIS 
	
	$QCallMain = "SELECT * FROM exchange_operations 
	WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}' 
	AND main_office_ref = '{$office}' 
	AND status = 'PAYED' 
	GROUP BY type_ref";
	$CallMain = $GLOBALS['CONN']->db_array($QCallMain);
	for ($m = 1; $m <= $CallMain[0]; $m++) { 
	$Total = 0;
	?>
			<tr bgcolor="#E2E2E2">
          <td colspan="2" valign="top">
		  <?php if ($CallMain[$m]['type_ref'] == '1') { print "Compra"; } elseif ($CallMain[$m]['type_ref'] == '2') { print "Venda";} ?></td>
       </tr>
	
<?php
	if ($m == 1) {$condition = 'out_currency_ref';} elseif ($m == 2) {$condition = 'in_currency_ref';} 
	if ($m == 1) {$condition2 = 'in_currency_ref';} elseif ($m == 2) {$condition2 = 'out_currency_ref';} 
	$QCallSub = "SELECT * FROM exchange_operations 
	WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}' 
	AND main_office_ref = '{$office}'
	AND type_ref = '{$m}' 
	AND status = 'PAYED'
	GROUP BY {$condition2}";
	$CallSub = $GLOBALS['CONN']->db_array($QCallSub);
	for ($n = 1; $n <= $CallSub[0]; $n++) { ?>
		<tr bgcolor="#F2F2F2">
          <td valign="bottom" bgcolor="#F2F2F2"><?php echo currency_output($CallSub[$n][$condition2],'icon')." ".currency_output($CallSub[$n][$condition2],'name');?> <?php echo $CSS['lines'];?></td>
    <td width="60%">
	<?php
			$QCallTotalAmount = "SELECT SUM(Amount) as Amount, tax1, operation_time, in_currency_ref, out_currency_ref FROM exchange_operations
			WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}' 
			AND main_office_ref = '{$office}' 
			AND type_ref = '{$m}' 
			AND status = 'PAYED'
			AND {$condition} = '{$CallSub[$n][$condition]}' AND {$condition2} = '{$CallSub[$n][$condition2]}'
			GROUP BY tax1";
			$CallTotalAmount = $GLOBALS['CONN']->db_array($QCallTotalAmount);
			for($x = 1; $x <= $CallTotalAmount[0]; $x++) {
			
			// Cacula Totais
		if ($m == 1 && $condition == "in_currency_ref") {$taxcalc = "buy1";}
		elseif ($m == 1 && $condition2 == "in_currency_ref") {$taxcalc = "buy2";}
		elseif ($m == 2 && $condition == "in_currency_ref") {$taxcalc = "sell1";}
		elseif ($m == 2 && $condition2 == "in_currency_ref") {$taxcalc = "sell2";}
		
		if ($condition == "in_currency_ref") {$regcalc = $CallTotalAmount[$x]['in_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['out_currency_ref'];
		}
		elseif ($condition == "out_currency_ref") {$regcalc = $CallTotalAmount[$x]['out_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['in_currency_ref'];
		}
		$QCallTot = "SELECT * FROM exchange_rates
			WHERE rate_time <= '{$CallTotalAmount[$x]['operation_time']}'
			AND (ref_currency1 = '{$regcalc}' AND ref_currency2 = '{$regcalc2}')
			OR (ref_currency2 = '{$regcalc}' AND ref_currency1 = '{$regcalc2}')
			ORDER BY rate_time DESC LIMIT 0,1";
			$CallTot = $GLOBALS['CONN']->db_array($QCallTot);
	 $minitot = $CallTotalAmount[$x]['Amount']*$CallTot[1][$taxcalc];
	 $Total = $Total+$minitot;
	 ?>
	 Total Recebido a <?php echo $CallTotalAmount[$x]['tax1'];?> : <?php echo $CallTotalAmount[$x]['Amount'];?><?php echo currency_output($CallSub[$n][$condition2],'icon')." ".currency_output($CallSub[$n][$condition2],'name');?>
	
	<br>
	<?php }
			$QCallTotalPayed = "SELECT SUM(acerto) as acerto, tax1 FROM exchange_operations
			WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}'
			AND main_office_ref = '{$office}' 
			AND type_ref = '{$m}' 
			AND status = 'PAYED'
			AND {$condition} = '{$CallSub[$n][$condition]}' AND {$condition2} = '{$CallSub[$n][$condition2]}'
			GROUP BY tax1";
			$CallTotalPayed = $GLOBALS['CONN']->db_array($QCallTotalPayed);
			for($y = 1; $y <= $CallTotalPayed[0]; $y++) {
			$CallTotalPayed[$y]['tax1']*$CallTotalPayed[$y]['acerto']; 
	 ?>
	 Total Gasto a <?php echo $CallTotalPayed[$y]['tax1'];?> : <?php echo $CallTotalPayed[$y]['acerto'];?><?php echo currency_output($CallMain[$m][$condition],'icon')." ".currency_output($CallMain[$m][$condition],'name');?>
	 <br>
		 

	<?php }
	} ?>
    <?php echo $CSS['lines'];?>
</td>
	    </tr>
	<?php 
	?>
	<tr bgcolor="#F2F2F2">
          <td valign="top">
	Total: <?php echo number_format($Total,2);?><?php echo currency_output($CallMain[$m][$condition],'icon')." ".currency_output($CallMain[$m][$condition],'name');?>
    <?php echo $CSS['lines'];?>				 
		  </td>
       
      </tr>
	<?php }
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//ALL DETAILS
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////?>
    </table>
<?php }
	
	function alldetails($time_ini,$time_end,$CSS) { ?>
	
	<table width="100%"  border="0" cellpadding="10" cellspacing="1">
	  <tr bgcolor="#009999">
          <td colspan="8">Detalhes de OperaÇ&otilde;es | Entre <?php echo date_output($time_ini)." e ".date_output($time_end);?></td>
      </tr>
	 
	  
	<?php
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Chama Datas e Lojas
	$TotalTOTAL = 0;
	$MovimentosTOTAL = 0;
	$dayantigo = 0;
	$QCallDaysOrder = "SELECT * FROM exchange_operations
	WHERE operation_time BETWEEN '{$time_ini}' AND '{$time_end}' 
	AND status = 'PAYED'
	ORDER BY operation_time ASC";
	$QCallDaysOrder = $GLOBALS['CONN']->db_array($QCallDaysOrder);
	for ($t = 1; $t <= $QCallDaysOrder[0]; $t++) { 
	$daystart = mktime(0,0,0,m($QCallDaysOrder[$t]['operation_time']),d($QCallDaysOrder[$t]['operation_time']),a($QCallDaysOrder[$t]['operation_time']));
	$dayend = mktime(24,60,60,m($QCallDaysOrder[$t]['operation_time']),d($QCallDaysOrder[$t]['operation_time']),a($QCallDaysOrder[$t]['operation_time']));

	
	if ($daystart != $dayantigo) {
	$dayantigo = $daystart;
	$QCallGroupOrder = "SELECT * FROM exchange_operations
	WHERE operation_time BETWEEN '{$daystart}' AND '{$dayend}' AND
	status = 'PAYED'
	GROUP BY main_office_ref ORDER BY main_office_ref ASC";
	
	$CallGroupOrder = $GLOBALS['CONN']->db_array($QCallGroupOrder);
	for ($e = 1; $e <= $CallGroupOrder[0]; $e++) { ?>
	<tr bgcolor="#0099CC"><td colspan="8"><b><?php if ($e == 1) {
	print date_output($daystart);
	} ?></b></td></tr>
		
	<tr bgcolor="#E2E2E2">
          <td valign="top" colspan="6">
    <?php 
	$QOfficeList = "SELECT * FROM main_offices WHERE ref = '{$CallGroupOrder[$e]['main_office_ref']}'";
	$OfficeList = $GLOBALS['CONN']->db_array($QOfficeList);
	
	print $OfficeList[1]['name']." | ".$OfficeList[1]['office_number']." | ".$OfficeList[1]['address_city'];
	
	?>
       <?php echo $CSS['lines'];?>          </td>
          <td valign="top">
		  <tr bgcolor="#E2E2E2">
          <td valign="top">Hora</td>
          <td valign="top">Recibo</td>
          <td valign="top">Tipo</td>
          <td valign="top">Cliente</td>
          <td valign="top">Taxa</td>
          <td valign="top">Valor Recebido </td>
          <td valign="top">Valor Entregue </td>
        </tr>
		<?php
	$QCallMainDetalhes = "SELECT * FROM exchange_operations
	WHERE operation_time BETWEEN '{$daystart}' AND '{$dayend}' AND main_office_ref = '{$OfficeList[1]['ref']}' AND status = 'PAYED'
	 ORDER BY operation_time,control_ref ASC";
	
	$CallMainDetalhes = $GLOBALS['CONN']->db_array($QCallMainDetalhes);
	$Movimentos = count($CallMainDetalhes)-1;
	$MovimentosTOTAL = $MovimentosTOTAL+$Movimentos;
	for ($m = 1; $m <= $CallMainDetalhes[0]; $m++) { ?>
        <tr bgcolor="#F9F9F9">
          <td valign="top">
            <?php echo hour_output($CallMainDetalhes[$m]['operation_time']);?>
            <?php echo $CSS['lines'];?>          </td>
          <td valign="top">
            <?php
	$QOffice = "SELECT * FROM main_offices WHERE ref = '{$CallMainDetalhes[$m]['main_office_ref']}'";
	$Office = $GLOBALS['CONN']->db_array($QOffice);
			$Office[1]['office_number'];?><?php echo $CallMainDetalhes[$m]['control_ref'];?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php if($CallMainDetalhes[$m]['type_ref'] = "1") { print "Compra";} else {
			print "Venda";} ?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top">
	<?php
	$QCallCli = "SELECT * FROM main_clients WHERE ref = '{$CallMainDetalhes[$m]['main_clients_ref']}'";
	$Cli = $GLOBALS['CONN']->db_array($QCallCli);
	print $Cli[1]['client_number'];
		  ?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php echo $CallMainDetalhes[$m]['tax1'];?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php echo $CallMainDetalhes[$m]['Amount'].currency_output($CallMainDetalhes[$m]['in_currency_ref'],"icon");?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php echo $CallMainDetalhes[$m]['acerto'].currency_output($CallMainDetalhes[$m]['out_currency_ref'],"icon");?>
            <?php echo $CSS['lines'];?></td>
        </tr>
        <?php } 
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// VISTA POR LOJA | TOTAIS 
	
	$QCallMain = "SELECT * FROM exchange_operations WHERE operation_time BETWEEN '{$daystart}' AND '{$dayend}' 
	AND main_office_ref = '{$OfficeList[1]['ref']}' 
	AND status = 'PAYED'
	GROUP BY type_ref";
	$CallMain = $GLOBALS['CONN']->db_array($QCallMain);
	for ($m = 1; $m <= $CallMain[0]; $m++) { 
	$Total = 0;

	if ($m == 1) {$condition = 'out_currency_ref';} elseif ($m == 2) {$condition = 'in_currency_ref';} 
	if ($m == 1) {$condition2 = 'in_currency_ref';} elseif ($m == 2) {$condition2 = 'out_currency_ref';} 
	$QCallSub = "SELECT * FROM exchange_operations 
	WHERE operation_time BETWEEN '{$daystart}' AND '{$dayend}'
	AND main_office_ref = '{$OfficeList[1]['ref']}'
	AND type_ref = '{$m}' 
	AND status = 'PAYED'
	GROUP BY {$condition2}";
	$CallSub = $GLOBALS['CONN']->db_array($QCallSub);
	for ($n = 1; $n <= $CallSub[0]; $n++) { ?>
		<tr bgcolor="#F2F2F2">
          <td colspan="4" valign="bottom" bgcolor="#F2F2F2"><?php echo currency_output($CallSub[$n][$condition2],'icon')." ".currency_output($CallSub[$n][$condition2],'name');?> <?php echo $CSS['lines'];?></td>
    <td colspan="4" width="60%">
	<?php
			$QCallTotalAmount = "SELECT SUM(Amount) as Amount, tax1, operation_time, in_currency_ref, out_currency_ref FROM exchange_operations
			WHERE operation_time BETWEEN '{$daystart}' AND '{$dayend}' 
			AND type_ref = '{$m}' 
			AND main_office_ref = '{$OfficeList[1]['ref']}'
			AND {$condition} = '{$CallSub[$n][$condition]}' AND {$condition2} = '{$CallSub[$n][$condition2]}'
			AND status = 'PAYED'
			GROUP BY tax1";
			$CallTotalAmount = $GLOBALS['CONN']->db_array($QCallTotalAmount);
			for($x = 1; $x <= $CallTotalAmount[0]; $x++) {
			// Cacula Totais
		if ($m == 1 && $condition == "in_currency_ref") {$taxcalc = "buy1";}
		elseif ($m == 1 && $condition2 == "in_currency_ref") {$taxcalc = "buy2";}
		elseif ($m == 2 && $condition == "in_currency_ref") {$taxcalc = "sell1";}
		elseif ($m == 2 && $condition2 == "in_currency_ref") {$taxcalc = "sell2";}
		
		if ($condition == "in_currency_ref") {$regcalc = $CallTotalAmount[$x]['in_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['out_currency_ref'];
		}
		elseif ($condition == "out_currency_ref") {$regcalc = $CallTotalAmount[$x]['out_currency_ref'];
		$regcalc2 = $CallTotalAmount[$x]['in_currency_ref'];
		}
		$QCallTot = "SELECT * FROM exchange_rates
			WHERE rate_time <= '{$CallTotalAmount[$x]['operation_time']}'
			AND (ref_currency1 = '{$regcalc}' AND ref_currency2 = '{$regcalc2}')
			OR (ref_currency2 = '{$regcalc}' AND ref_currency1 = '{$regcalc2}')
			ORDER BY rate_time DESC LIMIT 0,1";
			$CallTot = $GLOBALS['CONN']->db_array($QCallTot);
	 $minitot = $CallTotalAmount[$x]['Amount']*$CallTot[1][$taxcalc];
	 $Total = $Total+$minitot;
	 $TotalTOTAL = $Total+$TotalTOTAL;
	 ?>
	 Total Recebido a <?php echo $CallTotalAmount[$x]['tax1'];?> : <?php echo $CallTotalAmount[$x]['Amount'];?><?php echo currency_output($CallSub[$n][$condition2],'icon')." ".currency_output($CallSub[$n][$condition2],'name');?>
	
	<br>
	<?php }
			$QCallTotalPayed = "SELECT SUM(acerto) as acerto, tax1 FROM exchange_operations
			WHERE operation_time BETWEEN '{$daystart}' AND '{$dayend}' 
			AND main_office_ref = '{$OfficeList[1]['ref']}'
			AND type_ref = '{$m}' 
			AND {$condition} = '{$CallSub[$n][$condition]}' AND {$condition2} = '{$CallSub[$n][$condition2]}'
			AND status = 'PAYED'
			GROUP BY tax1";
			$CallTotalPayed = $GLOBALS['CONN']->db_array($QCallTotalPayed);
			for($y = 1; $y <= $CallTotalPayed[0]; $y++) {
			$CallTotalPayed[$y]['tax1']*$CallTotalPayed[$y]['acerto']; 
	 ?>
	 Total Gasto a <?php echo $CallTotalPayed[$y]['tax1'];?> : <?php echo $CallTotalPayed[$y]['acerto'];?><?php echo currency_output($CallMain[$m][$condition],'icon')." ".currency_output($CallMain[$m][$condition],'name');?>
	<br>
		 

	<?php } ?>
    <?php echo $CSS['lines'];?>
</td>
	    </tr>
	<?php 
	} ?>
	<tr bgcolor="#F2F2F2">
          <td colspan="8" valign="top">
	<b>N�mero de Movimentos: <?php echo $Movimentos;?> | Total: <?php echo number_format($Total,2);?>
	<?php echo currency_output($CallMain[$m][$condition],'icon')." ".currency_output($CallMain[$m][$condition],'name');?>
    </b><?php echo $CSS['lines'];?>				 
		  </td>
       
      </tr>
	<?php }
		}
	 } 
	 } ?>
	 <tr bgcolor="#F2F2F2">
          <td colspan="8" valign="top">
	<b>VALORES GERAIS : N�mero de Movimentos: <?php echo $MovimentosTOTAL;?> | Total: <?php echo number_format($TotalTOTAL,2);?>
	<?php echo currency_output($CallMain[1][$condition],'icon')." ".currency_output($CallMain[1][$condition],'name');?>
    </b><?php echo $CSS['lines'];?>				 
		  </td>
       
      </tr>
	<?php 
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//END ALL DETAILS
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	 ?>
    </table>
	<?php 
		
		}
	function details($time_ini,$time_end,$office,$CSS) { ?>
	
	<table width="100%"  border="0" cellpadding="10" cellspacing="1">

 	<?php
	$QCallSubLoja = "SELECT * FROM main_offices
	WHERE ref = '{$office}'";
	$CallSubLoja = $GLOBALS['CONN']->db_array($QCallSubLoja); ?>
	  <tr bgcolor="#009999">
          <td colspan="7">Detalhes de OperaÇ&otilde;es | Loja: <?php echo $CallSubLoja[1]['address_city'];?></td>
      </tr>
		        <tr bgcolor="#E2E2E2">
          <td valign="top">Data</td>
          <td valign="top">Recibo</td>
          <td valign="top">Tipo</td>
          <td valign="top">Cliente</td>
          <td valign="top">Taxa</td>
          <td valign="top">Valor Recebido </td>
          <td valign="top">Valor Entregue </td>
        </tr>
		<?php
	$QCallMainDetalhes = "SELECT * FROM exchange_operations
	WHERE main_office_ref = '{$office}'
	AND operation_time BETWEEN '{$time_ini}' AND '{$time_end}' AND status = 'PAYED'";
	
	$CallMainDetalhes = $GLOBALS['CONN']->db_array($QCallMainDetalhes);
	for ($m = 1; $m <= $CallMainDetalhes[0]; $m++) { ?>
        <tr bgcolor="#F9F9F9">
          <td valign="top">
            <?php echo time_output($CallMainDetalhes[$m]['operation_time']);?>
            <?php echo $CSS['lines'];?>          </td>
          <td valign="top">
            <?php echo $CallSubLoja[1]['office_number'];?><?php echo $CallMainDetalhes[$m]['control_ref'];?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php if($CallMainDetalhes[$m]['type_ref'] = "1") { print "Compra";} else {
			print "Venda";} ?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top">
	<?php
	$QCallCli = "SELECT * FROM main_clients WHERE ref = '{$CallMainDetalhes[$m]['main_clients_ref']}'";
	$Cli = $GLOBALS['CONN']->db_array($QCallCli);
	print $Cli[1]['client_number'];
		  ?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php echo $CallMainDetalhes[$m]['tax1'];?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php echo $CallMainDetalhes[$m]['Amount'].currency_output($CallMainDetalhes[$m]['in_currency_ref'],"icon");?>
            <?php echo $CSS['lines'];?></td>
          <td valign="top"><?php echo $CallMainDetalhes[$m]['acerto'].currency_output($CallMainDetalhes[$m]['out_currency_ref'],"icon");?>
            <?php echo $CSS['lines'];?></td>
        </tr>
        <?php } ?>
</table>
<?php 
} ?>
